package com.stbu.bean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;

@Controller
public class FileController {


    @RequestMapping("/toUpload")
    public String toUpload(){
        return "upload";
    }
    @RequestMapping("/upload")
    public ModelAndView Upload(@RequestPart("myFile")MultipartFile file){
        String destStr ="D:"+ File.separator+"FileIo"+File.separator;
      File  destFile =new File(destStr);
        if(!destFile.exists()){
            destFile.mkdir();
        }
        System.out.println("目标路径"+destFile.getAbsolutePath());
        File rootFile=new File(destFile,file.getOriginalFilename());
        System.out.println("目标全路径"+rootFile.getAbsolutePath());
        String msg="";
        try{
            file.transferTo(rootFile);
            msg="上传成功";
        }catch (IOException e){
            e.printStackTrace();
            msg="上传失败";
        }
        ModelAndView view =new ModelAndView();
        view.addObject("msg",msg);
        view.setViewName("info");
        return view;
    }
}
